<template>
    <el-form :model="testForm" :rules="rules" ref="testForm" label-width="100px">
      <el-form-item label="测试名称" prop="name">
        <el-input v-model="testForm.name" placeholder="请输入测试名称" style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item label="测试班级" prop="classId">
        <el-select  v-model="testForm.classId" placeholder="请选择测试班级" clearable filterable>
          <el-option v-for="item in classOptions" :key="item.id" :value="item.id" :label="item.className"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="测试课程" prop="courseId">
        <el-select  v-model="testForm.courseId" placeholder="请选择测试课程" clearable filterable @change="getSectionOptionsByCourseId">
          <el-option v-for="item in courseOption" :key="item.id" :value="item.id" :label="item.courseName"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="测试章节" prop="sectionId">
        <el-select  v-model="testForm.sectionId" placeholder="请选择测试章节" clearable filterable>
          <el-option v-for="item in sectionOptions" :key="item.id" :value="item.id" :label="item.courseSection"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input style="width: 200px" v-model="testForm.remark" type="textarea" :autosize="{ minRows: 3, maxRows: 20}"/>
      </el-form-item>
      <el-form-item style="text-align: center" label-width="0">
        <el-button type="primary" @click="submitForm">保存</el-button>
      </el-form-item>
    </el-form>
</template>

<script>
import { getTest,editTest } from '@/api/exam/classroomTest'
import { listAllBClass } from '@/api/basedata/bclass'
import { findAllCourse } from '@/api/basedata/bcourse'

export default {
  name : 'editTest',
  props: ["testId"],
  data()  {
    return {
      classOptions:[], //班级下拉框
      courseOption:[],// 课程下拉框
      sectionOptions:[],//章节下拉框

      testForm : { //表单内容
        id: null,
        name:'',
        classId:'',
        courseId:'',
        sectionId:'',
        remark: ''
      },
      rules: { //表单验证
        name: [
          {required: true, message: '请输入测试名称', trigger: 'blur'}
        ],
        classId: [
          {required: true, message: '请选择测试班级', trigger: 'change'}
        ],
        courseId: [
          {required: true, message: '请选择测试课程', trigger: 'change'}
        ],
        sectionId: [
          {required: true, message: '请选择测试章节', trigger: 'change'}
        ],
      },
    }
  },
  async created() {
    await findAllCourse().then((result) => {
      this.courseOption = result.data
    })

    listAllBClass().then((resp) => {
      this.classOptions = resp.data;
    });

    if(this.testId !=='') {
      getTest(this.testId).then(response => {
        this.testForm = response.data
        this.getSectionOptionsByCourseId(this.testForm.courseId);
      })
    }
  },
  methods : {
    //表单提交
    submitForm() {
      this.$refs.testForm.validate((valid) => {
        if (valid) {
          editTest(this.testForm).then(()=> {
            this.$message.success('保存成功！');
            this.$emit('editTestSuccess');
          })
        }
      });
    },
    //根据课程id获取对应章节
    getSectionOptionsByCourseId(id) {
      this.sectionOptions = [];
      const courseItem = this.courseOption.find(item => item.id === id);
      if (courseItem) {
        this.sectionOptions = courseItem.sectionList;
      }
    }
  }
}
</script>

<style scoped>

</style>
